package com.hnust.controller;

import com.hnust.dao.ChapterDao;
import com.hnust.domain.ChapterData;
import com.hnust.domain.ChapterDataRecord;
import com.hnust.util.IDUtil;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Yjx
 * @Data: 2021/10/31 19:17
 * @Version 1.0
 * @Project_Name: teacherSystemBackEnd-master
 * @describe
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    ChapterDao chapterDao;

    @PostMapping("/allImport")
    Result<String> allImportChapter(@RequestBody ChapterDataRecord data,@RequestParam("courseId") String courseId){
        Integer integer = 1;
        List<ChapterData> list = data.getList();
        for(int i = 0;i<list.size();i++){
//            System.out.println(list.get(i).toString());
            integer = chapterDao.insertChapter(IDUtil.getPrimaryKey(), list.get(i).getName(), Integer.parseInt(list.get(i).getChapter()), Integer.parseInt(list.get(i).getFrist()), Integer.parseInt(list.get(i).getSecond()), Integer.parseInt(list.get(i).getThird()), courseId);
            System.out.println(integer);
        }
        if(integer == null){
            return new Result<>(0,"导入失败，可能是有重复项","成功");
        }else {
            return new Result<>(1,"导入成功","成功");
        }

    }
}
